<?php
/**
 * Amigos
 *
 * amigos.php
 * @author Hector Lucero
 * @version 1.0
 * @copyright Hector Lucero, 13 October, 2011
 * @package default
 **/

class Amigos extends CI_Controller {
    
    public function __construct() {
        parent::__construct();

        $this->load->model("menu_model");
        $this->load->model("amigos_model");

        //Only logged in users are alowed to access this controller
        if($this->session->userdata('logged_in') == FALSE) redirect('login');
    }

    function index() {
        $data = $this->menu_model->general("Amigos");
        $data['content_html'] = $this->dashboard();
        $this->load->view('template', $data);
    }

    function dashboard() {
        $order = array('apellido', 'nombre');
        $html = "";
        $html.= "<table border=1>";
        $html.= "<thead>";
        $html.= "<tr bgcolor='#CEE3F6'>";
        $html.= "<th>NOMBRE</th>";
        $html.= "<th>APELLIDO</th>";
        $html.= "<th>DIRECCION</th>";
        $html.= "<th>TELEFONO</th>";
        $html.= "<th>CELULAR</th>";
        $html.= "<th>CORREO ELECTRONICO</th>";
        $html.= "<tH colspan=2 size='6%'>".anchor('amigos/add','Nuevo Amigo')."</th>";
        $html.= "</tr>";
        $html.= "</thead>";
        $html.= "<tbody>";
        $values = $this->amigos_model->get_list('amigos','',$order);
        if(!empty($values)) {
            foreach($values as $k=>$v) {
                $id = $values[$k]['id'];
                $html.= "<tr>";
                $html.= "<td>".$values[$k]['nombre']."</td>";
                $html.= "<td>".$values[$k]['apellido']."</td>";
                $html.= "<td>".$values[$k]['direccion']."</td>";
                $html.= "<td>".$values[$k]['telefono']."</td>";
                $html.= "<td>".$values[$k]['celular']."</td>";
                $html.= "<td>".$values[$k]['email']."</td>";
                $html.= "<td>".anchor('amigos/add/'.$id,'Edit')."</td>";
                $html.= "<td>".anchor('amigos/delete/'.$id,'Delete')."</td>";
                $html.= "</tr>";
            }
        }
        $html.= "</tbody>";
        $html.= "</table>";

        return $html;
    }

    function add($id=0) {
        $data = $this->menu_model->general("Amigos");

        $form_path = "amigos/add/".$id;
        $path = "amigos/index";
        $table = "amigos";
        $id_name = "id";
        
        $fields = $this->amigos_model->fields($table);
        $rules = $this->amigos_model->rules($table, $fields);
        
        if($this->form_validation->run() == FALSE) {
            $new = 1;
            $values = array();
            foreach($fields as $k=>$v) {
                $values[0][$k] = "";
            }
        
            $exists = $this->amigos_model->exists($table, $id_name, $id);
            if(!empty($exists)) {
                $new = 0;
                $values = $this->amigos_model->read($table, $id_name, $id);
            }
        
            $form = "<div>";
            $form .= "Los Campos en Rojo<span class='required'>labels</span> Son Requeridos!";
            $form .= "</div>";

            $form .= "<div>";
            $form .= validation_errors();
            $form .= "</div>";
        
            $form .= form_open($form_path);
            $form .= form_hidden($id_name, $id);
        
            $form .= form_label($fields["nombre"],"nombre");
            $form .= "<br/>";
            $form .= form_input(array("name" => "nombre", "id" => "nombre", "size" => "35"), set_value("nombre",$values[0]["nombre"]));
            $form .= "<br><br/>";

            $form .= form_label($fields["apellido"],"apellido");
            $form .= "<br/>";
            $form .= form_input(array("name" => "apellido", "id" => "apellido", "size" => "35"), set_value("apellido",$values[0]["apellido"]));
            $form .= "<br><br/>";

            $form .= form_label($fields["direccion"],"direccion");
            $form .= "<br/>";
            $form .= form_input(array("name" => "direccion", "id" => "direccion", "size" => "35"), set_value("direccion",$values[0]["direccion"]));
            $form .= "<br><br/>";

            $form .= form_label($fields["ciudad"],"ciudad");
            $form .= "<br/>";
            $form .= form_input(array("name" => "ciudad", "id" => "ciudad", "size" => "35"), set_value("ciudad",$values[0]["ciudad"]));
            $form .= "<br><br/>";

            $form .= form_label($fields["estado"],"estado");
            $form .= "<br/>";
            $form .= form_input(array("name" => "estado", "id" => "estado", "size" => "2"), set_value("estado",$values[0]["estado"]));
            $form .= "<br><br/>";

            $form .= form_label($fields["telefono"],"telefono");
            $form .= "<br/>";
            $form .= form_input(array("name" => "telefono", "id" => "telefono", "size" => "25"), set_value("telefono",$values[0]["telefono"]));
            $form .= "<br><br/>";

            $form .= form_label($fields["celular"],"celular");
            $form .= "<br/>";
            $form .= form_input(array("name" => "celular", "id" => "celular", "size" => "25"), set_value("celular",$values[0]["celular"]));
            $form .= "<br><br/>";

            $form .= form_label($fields["email"],"correo");
            $form .= "<br/>";
            $form .= form_input(array("name" => "email", "id" => "email", "size" => "35"), set_value("Correo Electronico",$values[0]["email"]));
            $form .= "<br><br/>";
            
            $form .= form_submit('submit', 'Submit') . nbs(2) . form_reset('reset', 'Reset');
        
            $form .= form_close();

            $data['content_html'] = $form;
            $this->load->view('template',$data);
        } else {
            $id = $this->input->post($id_name);
            $data = array();
            foreach($fields as $k=>$v) {
                $value = $this->input->post($k);
                $data[$k] = $value;
            }
            $this->amigos_model->save($table, $id_name, $id, $data);
            redirect($path);
        }

    }

    function delete($id=0) {
        if($id > 0) {
            $table = "amigos";
            $id_name = "id";
            $result = $this->amigos_model->delete($table, $id_name, $id);
        }
        redirect('amigos/index');
    }
}        

/* end of file */
